﻿using System.Web.Mvc;
using Journal.Admin.Presentation.FilterAttributes;
using Journal.Admin.Presentation.IPresentationServices;
using Journal.Admin.Presentation.Models.UserModels;
using Journal.Common.Objects.Enums;

namespace Journal.Admin.Presentation.Controllers
{
    [Permission(Roles.Admin, new[] { Permissions.Journal })]
    public class UserController : Controller
    {
        private readonly IUserPresentationService userPresentationService;

        public UserController(IUserPresentationService userPresentationService)
        {
            this.userPresentationService = userPresentationService;
        }

        [HttpGet]
        public ActionResult Users()
        {
            UserSelectModel model = userPresentationService.GetSelectModel();
            return View(model);
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View("Edit", userPresentationService.CreateEditModel());
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            UserEditModel model = userPresentationService.GetUserById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(UserEditModel model)
        {
            if (ModelState.IsValid)
            {
                userPresentationService.Save(model);
                return RedirectToAction("Users");
            }

            return View("Edit", model);
        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            userPresentationService.Delete(id);
            return RedirectToAction("Select");
        }
    }
}